function saveAlign(obj: egret.DisplayObject): any {
	return obj instanceof eui.Component
		? {
				left: obj.left,

				right: obj.right,

				top: obj.top,

				bottom: obj.bottom,

				verticalCenter: obj.verticalCenter,

				horizontalCenter: obj.horizontalCenter,
		  }
		: {};
}

function restoreAlign(obj: egret.DisplayObject, data: any): void {
	if (obj instanceof eui.Component) {
		obj.left = data.left;
		obj.right = data.right;
		obj.top = data.top;
		obj.bottom = data.bottom;
		obj.verticalCenter = data.verticalCenter;
		obj.horizontalCenter = data.horizontalCenter;
	}
}

function clearAlign(obj: egret.DisplayObject): void {
	restoreAlign(obj, {});
}

const needClearEffects = ['leftOut', 'leftIn'];

function needClearEffect(effect: string): boolean {
	return needClearEffects.indexOf(effect) >= 0;
}

class EffectUI {
	obj: egret.DisplayObject;

	x: number;

	y: number;

	data: any;

	constructor(obj: egret.DisplayObject) {
		this.obj = obj;
		this.x = obj.x;
		this.y = obj.y;
		this.data = saveAlign(obj);
	}

	_setEffect(effect: string, timems: number): egret.Tween {
		// console.log('setEffectUI', effect, this.obj.x, this.obj.y, this.obj.width, this.obj.height);
		const win = this.obj;
		switch (effect) {
			case 'leftOut': {
				const x = -this.obj.width;
				const tw = egret.Tween.get(this.obj).to({ x }, timems);
				return tw;
			}
			case 'leftIn': {
				const { x } = this.obj;
				this.obj.x = -this.obj.width;
				const tw = egret.Tween.get(this.obj).to({ x }, timems);
				return tw;
			}
			case 'popUp': {
				// 不设置锚点，这样扩展性强一点
				// win.anchorOffsetX = win.width * 0.5;
				// win.anchorOffsetY = win.height * 0.5;
				win.scaleX = 0.5;
				win.scaleY = 0.5;
				return egret.Tween.get(win).to(
					{ scaleX: 1, scaleY: 1 },
					timems,
					egret.Ease.backOut,
				);
			}
			case 'popDown': {
				return egret.Tween.get(win).to({ scaleX: 0.5, scaleY: 0.5 }, timems);
			}

			case 'alphaUp': {
				console.log('set eft alphaup');
				win.alpha = 0;
				return egret.Tween.get(win).to({ alpha: 1 }, timems);
			}

			case 'alphaDown': {
				return egret.Tween.get(win).to({ alpha: 0 }, timems);
			}
			default:
		}
		return null;
	}

	setEffect(effect: string, timems: number): egret.Tween {
		const tw = this._setEffect(effect, timems);
		if (tw) {
			// console.log('clearAlign');
			const needclear = needClearEffect(effect);
			if (needclear) {
				clearAlign(this.obj);
			}
			tw.call(() => {
				// console.log('restoreAlign');
				if (needclear) {
					restoreAlign(this.obj, this.data);
				}
				// this.obj.x = 100;
			});
		}
		return tw;
	}
}

function setEffectUI(obj: egret.DisplayObject, effect: string, timems: number): egret.Tween {
	const num = new EffectUI(obj);
	const tw = num.setEffect(effect, timems);
	return tw;
}

function asyncSetEffectUI(obj: egret.DisplayObject, effect: string, timems: number): Promise<void> {
	return new Promise((resolve, reject) => {
		const tw = setEffectUI(obj, effect, timems);
		if (tw) {
			tw.call(resolve);
		} else {
			reject();
		}
	});
}
